package com.bookmall.cart.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookmall.cart.domain.OrderPaymentInfo;
import com.bookmall.cart.domain.vo.MemberAddressVo;
import com.bookmall.cart.feign.MemberFeignService;
import com.bookmall.cart.interceptor.CartInterceptor;
import com.bookmall.cart.mapper.OrderPaymentInfoMapper;
import com.bookmall.cart.service.IOrderInfoItemService;
import com.bookmall.cart.service.IOrderInfoService;
import com.bookmall.cart.service.IOrderPaymentInfoService;
import com.bookmall.common.entity.to.MemberInfoTo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-21-11:24
 */
@Service
public class OrderPaymentInfoServiceImpl extends ServiceImpl<OrderPaymentInfoMapper, OrderPaymentInfo> implements IOrderPaymentInfoService {
    @Autowired
    private MemberFeignService memberFeignService;
    /**
     * 添加一个新的收货地址
     * @author 胡菜鸡
     * @date 2022/4/22 15:41
     */
    @Override
    public int addNewAddress(MemberAddressVo memberAddressVo) {
        MemberInfoTo memberInfoTo = CartInterceptor.threadLocal.get();
        memberAddressVo.setMemberId(memberInfoTo.getId());
        if(memberAddressVo.getDefaultStatus() != null){
            memberAddressVo.setDefaultStatus(memberAddressVo.getDefaultStatus().equalsIgnoreCase("on")?"0":"1");
        } else {
            memberAddressVo.setDefaultStatus("1");
        }
        AjaxResult ajaxResult = memberFeignService.add(memberAddressVo);
        return ajaxResult.get(AjaxResult.CODE_TAG).equals("200") ? 1 : 0;
    }

    /**
     * 根据收货地址计算运费（模拟该功能，实际需要结合第三方快递服务的接口）
     * @author 胡菜鸡
     * @date 2022/4/24 16:02
     */
    @Override
    public BigDecimal getFare(String addressId) {
        AjaxResult ajaxResult = memberFeignService.getAddressInfo(addressId);
        MemberAddressVo memberAddressVo = ajaxResult.getData("data", new TypeReference<MemberAddressVo>() {
        });
        if(memberAddressVo != null){
            // 182 1111 2222
            String phone = memberAddressVo.getPhone();
            String lastNumber = phone.substring(phone.length() - 1);
            return new BigDecimal(lastNumber);
        }
        return null;
    }
}
